package dto

import (
	"github.com/google/uuid"
	"time"
	"vrcm/src/vrcm/app/admin/models/domian"
	"vrcm/src/vrcm/common/dto"
	common "vrcm/src/vrcm/common/models"
)

type CertificateGetPageReq struct {
	dto.Pagination   `search:"-"`
	TenantId         string `json:"tenant_id"  search:"type:exact;column:tenant_id;table:certificate"`
	CertificateName  string `json:"certificate_name"  search:"type:contains;column:certificate_name;table:certificate"`
	CertificateState int    `json:"certificate_state"  search:"-"`
	AuthorizedDomain string `json:"authorized_domain"  search:"-"`
}

type CertificateOrder struct {
	Id               string `form:"idOrder"  search:"type:order;column:id;table:certificate"`
	TenantId         string `form:"tenant_idOrder"  search:"type:order;column:tenant_id;table:certificate"`
	CertificateName  string `form:"certificate_name"  search:"type:order;column:certificate_name"`
	PublicKey        string `form:"public_keyOrder"  search:"type:order;column:public_key;table:certificate"`
	PrivateKey       string `form:"private_keyOrder"  search:"type:order;column:private_key;table:certificate"`
	CertificateState string `form:"certificate_stateOrder"  search:"type:order;column:certificate_state;table:certificate"`
	CreateTime       string `form:"createTimeOrder"  search:"type:order;column:create_time;table:certificate"`
	UpdateTime       string `form:"updateTimeOrder"  search:"type:order;column:update_time;table:certificate"`
	CreateBy         string `form:"createByOrder"  search:"type:order;column:create_by;table:certificate"`
	UpdateBy         string `form:"updateByOrder"  search:"type:order;column:update_by;table:certificate"`
}

func (m *CertificateGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type CertificateInsertReq struct {
	Id              int    `uri:"id" comment:""` //
	TenantId        string `json:"tenant_id" comment:"租户"`
	DomainId        int    `json:"domain_id"`
	CertificateName string `json:"certificate_name" comment:"证书名称"`
	PublicKey       string `json:"public_key" comment:"公钥"`
	PrivateKey      string `json:"private_key" comment:"私钥"`
	CertId          string `json:"cert_id" comment:""`
	//Domains         []int  `json:"domains" comment:"绑定的域名"`
	common.ControlBy
}

func (s *CertificateInsertReq) Generate(model *domain.Certificate) {
	now := time.Now().Unix()
	model.TenantId = s.TenantId
	model.CertificateName = s.CertificateName
	model.PublicKey = s.PublicKey
	model.PrivateKey = s.PrivateKey
	model.CertificateState = 1
	model.CreateTime = now
	model.UpdateTime = now
	model.CertificateId = uuid.New().String()
	model.CertificateState = 1
	model.CertId = uuid.New().String()
	model.CreateBy = s.CreateBy
}

type CertificateUpdateReq struct {
	Id               int    `json:"id" comment:""` //
	TenantId         string `json:"tenant_id" comment:"租户"`
	DomainId         int    `json:"domain_id"`
	CertificateName  string `json:"certificate_name" comment:"证书名称"`
	PublicKey        string `json:"public_key" comment:"公钥"`
	PrivateKey       string `json:"private_key" comment:"私钥"`
	CertificateState string `json:"certificate_state" comment:"1正常 2异常 3已过期"`
	CertId           string `json:"cert_id"`
	//Domains          []int  `json:"domains" comment:"绑定的域名"`
	common.ControlBy
}

func (s *CertificateUpdateReq) Generate(model *domain.Certificate) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	now := time.Now().Unix()
	model.TenantId = s.TenantId
	model.CertificateName = s.CertificateName
	model.CreateTime = now
	model.UpdateTime = now
	model.CertificateId = uuid.New().String()
	model.PublicKey = s.PublicKey
	model.PrivateKey = s.PrivateKey
	model.UpdateTime = now
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
	if len(s.CertId) > 0 {
		model.CertId = s.CertId
	}
}

func (s *CertificateUpdateReq) GetId() interface{} {
	return s.Id
}

// CertificateGetReq 功能获取请求参数
type CertificateGetReq struct {
	Id int `uri:"id"`
}

type CertificateGetByCertIdReq struct {
	CertId string `json:"cert_id"`
}

type GetByDomainReq struct {
	TenantId string `json:"tenant_id" comment:"租户"`
	Domain   string `json:"domain"`
}

func (s *CertificateGetReq) GetId() interface{} {
	return s.Id
}

// CertificateDeleteReq 功能删除请求参数
type CertificateDeleteReq struct {
	Ids []int `json:"ids"`
}

type CertificateRemoveReq struct {
	CertId string `json:"cert_id"`
}

func (s *CertificateDeleteReq) GetId() interface{} {
	return s.Ids
}

type RefreshSaveReq struct {
	TenantId     string   `json:"tenant_id"`
	OperateType  int      `json:"operate_type"`
	OperateModel int      `json:"operate_model"`
	Urls         []string `json:"urls"`
	DomainId     int      `json:"domain_id"`
}

type RefreshGetReq struct {
	TenantId    string `json:"tenant_id"`
	OperateType int    `json:"operate_type"`
	StartTime   int    `json:"start_time"`
	EndTime     int    `json:"end_time"`
	TaskId      string `json:"task_id"`
}

type CancelCertificateDomainReq struct {
	DomainId      int `json:"domain_id"`
	CertificateId int `json:"certificate_id" comment:"证书id"`
}

type RefreshRecordGetPageReq struct {
	dto.Pagination `search:"-"`
	TenantId       string `json:"tenant_id"  search:"type:exact;column:tenant_id;table:refresh_config"`
	TaskId         string `json:"task_id"  search:"type:exact;column:task_id;table:refresh_config"`
	Url            string `json:"url"  search:"type:contains;column:url;table:refresh_config"`
	RefreshState   int    `json:"refresh_state"  search:"-"`
	StartTime      int    `json:"start_time" search:"-"`
	EndTime        int    `json:"end_time"  search:"-"`
}

func (m *RefreshRecordGetPageReq) GetNeedSearch() interface{} {
	return *m
}
